<?php

namespace App\Http\Controllers\Back;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FinanceController extends Controller
{
    public function index(Request $request)
    {
        $type = 0;
//        if ($request->input('type')) {
//            $type = $request->input('type');
//        }
        if ($phone = $request->input('phone')) {
            $user = User::where('phone', $phone)->orWhere('account', $phone)->first();

            if ($user == NULL){

             return    $this->renderJson([], -1, '用户不存在');
            }
            if (isset($user->type)){
                $type = $user->type;
            }else{
                $type = $request->input('type');
            }
        }
        if ($start_create = $request->input('start_time')) {
            $start_create = date('Y-m-01 00:00:00');
        }

        if ($end_create = $request->input('end_time')) {
            $end_create = date('Y-m-d 23:59:59');
        }

        switch ($type) {
            // 总平台
            case 0:
                return $this->getGeneralPlatform($start_create, $end_create);
                break;
                // 城市合伙人
            case  1:
                return $this->getPlacePartnerFromAmountList($user->id, $start_create, $end_create);
                break;
                // 运营商
            case 2:
                return $this->getOperatorsFromAmountList($user->id, $start_create, $end_create);
                break;
                // 业务员
            case 3:
                return $this->getSalesmenFromAmountList($user->id, $start_create, $end_create);
                break;
                // 会员
            case 4:
                return $this->getUserFromAmountList($user->id, $start_create, $end_create);
                break;
        }

    }

    //todo  会员
    public function getUserFromAmountList($user_id, $start_create, $end_create)
    {
        $user = User::where('id', $user_id)->first();
        $oneUser = \DB::select('SELECT
                                            count(id) as totalNumber, 
                                            IFNULL(SUM(money), 0) as totalAmount
                                        FROM
                                            user_commission_details 
                                        WHERE
                                            relation_id = ?
                                            AND
                                            relation = 1
                                            AND created_at BETWEEN  ? AND ?', [$user_id, $start_create, $end_create]);
        $twoUser = \DB::select('SELECT
                                            count(id) as totalNumber, 
                                            IFNULL(SUM(money), 0) as totalAmount
                                        FROM
                                            user_commission_details 
                                        WHERE
                                            relation_id = ?
                                            AND
                                            relation = 2
                                            AND created_at BETWEEN  ? AND ?
                                                 ', [$user_id, $start_create, $end_create]);

        return $this->renderJson([
            'user_phone' => $user->phone,
            'oneUser' => $oneUser,
            'twoUser' => $twoUser,
            'start_time' => $start_create,
            'end_time' => $end_create
        ]);
    }

    // todo 业务员
    public function getSalesmenFromAmountList($user_id, $start_create, $end_create)
    {

        $user = User::where('id', $user_id)->first();
        $oneSalesman = \DB::select('SELECT
                                            count(id) as totalNumber, 
                                            IFNULL(SUM(money), 0) as totalAmount
                                        FROM
                                            user_commission_details 
                                        WHERE
                                            relation_id = ?
                                            AND
                                            relation = 3
                                            AND created_at BETWEEN  ? AND ? ',
            [$user_id, $start_create, $end_create]);
        $twoSalesman = \DB::select('SELECT
                                            count(id) as totalNumber, 
                                            IFNULL(SUM(money), 0) as totalAmount
                                        FROM
                                            user_commission_details 
                                        WHERE
                                            relation_id = ?
                                            AND
                                            relation = 4
                                            AND created_at BETWEEN  ? AND ?', [$user_id, $start_create, $end_create]);
        $threeSalesman = \DB::select('SELECT
                                            count(id) as totalNumber, 
                                            IFNULL(SUM(money), 0) as totalAmount
                                        FROM
                                            user_commission_details 
                                        WHERE
                                            relation_id = ?
                                            AND
                                            relation = 5
                                            AND created_at BETWEEN  ? AND ?
                                                 ', [$user_id, $start_create, $end_create]);

        return $this->renderJson([
            'user_phone' => $user->phone,
            'oneSalesman' => $oneSalesman,
            'twoSalesman' => $twoSalesman,
            'threeSalesman' => $threeSalesman,
            'start_time' => $start_create,
            'end_time' => $end_create
        ]);
    }

    // todo 运营商
    public function getOperatorsFromAmountList($user_id, $start_create, $end_create)
    {
        $user = User::where('id', $user_id)->first();
        $oprator = \DB::select('SELECT
                                        count(id) as totalNumber, 
                                        IFNULL(SUM(money), 0) as totalAmount
                                    FROM
                                        user_commission_details 
                                    WHERE
                                        relation_id = ?
                                        AND
                                        relation = 6
                                        AND created_at BETWEEN  ? AND ? ', [$user_id, $start_create, $end_create]);
        return $this->renderJson([
            'user_phone' => $user->phone,
            'users' => $oprator,
            'start_time' => $start_create,
            'end_time' => $end_create

        ]);
    }

    // todo 城市代理商
    public function getPlacePartnerFromAmountList($user_id, $start_create, $end_create)
    {
        $user = User::where('id', $user_id)->first();
        $placePartner = \DB::select('SELECT
                                                count(id) as totalNumber, 
                                                IFNULL(SUM(money),0) as totalAmount
                                            FROM
                                                user_commission_details 
                                            WHERE
                                                relation_id = ?
                                                AND 
                                                relation = 7
                                                AND created_at BETWEEN  ? AND ? ', [$user_id, $start_create, $end_create]);

        $operators = \DB::select('SELECT
                            count( a.id ) AS totalNumber,
                            IFNULL(SUM( b.agency_fee ),0) AS totalAmount 
                        FROM
                            users AS a
                            LEFT JOIN operators AS b ON a.id = b.user_id 
                        WHERE
                            b.place_partner_id = ?
                            AND a.created_at BETWEEN ? 
                            AND ?', [$user_id, $start_create, $end_create]);

        return $this->renderJson([
            'user_phone' => $user->phone,
            'users' => $placePartner,
            'operators' => $operators,
            'start_time' => $start_create,
            'end_time' => $end_create

        ]);
    }

    // todo 总平台
    public function getGeneralPlatform($start_create, $end_create)
    {

        $users = \DB::select('SELECT
                                    count(a.id) as totalNumber, 
                                    IFNULL(SUM(b.money), 0) as totalAmount
                                  
                                FROM
                                    users AS a
                                    LEFT JOIN user_recharges b ON a.id = b.id 
                                WHERE
                                    a.created_at BETWEEN ? 
                                    AND ? ', [$start_create, $end_create]);

        $operators = \DB::select('SELECT
                            count( a.id ) AS totalNumber,
                            IFNULL(SUM( b.agency_fee ),0) AS totalAmount 
                        FROM
                            users AS a
                            LEFT JOIN operators AS b ON a.id = b.user_id 
                        WHERE
                            a.created_at BETWEEN ? 
                            AND ?', [$start_create, $end_create]);
        return $this->renderJson([
            'users' => $users,
            'operators' => $operators,
            'start_time' => $start_create,
            'end_time' => $end_create
        ]);
    }
}
